/**
 * 
 */
package slhs.tempo.pivotTableUtilities;

import java.awt.Component;
import java.awt.Font;
import java.awt.LayoutManager;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.TableModel;

import org.openswing.swing.client.ExportButton;
import org.openswing.swing.client.FilterButton;
import org.openswing.swing.client.ReloadButton;
import org.openswing.swing.pivottable.client.PivotTable;
import org.openswing.swing.wizard.client.WizardInnerPanel;

import slhs.tempo.TableAnalyzer.TableModelDataConverter;
import slhs.tempo.TableAnalyzer.TablePanelHandler;
import slhs.tempo.scriptGenerator.UtilityConstants;

import com.jgoodies.forms.factories.ButtonBarFactory;
import com.jgoodies.forms.factories.DefaultComponentFactory;

/**
 * PivotWizardPostAnalysisPivotPanel.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class PivotWizardPostAnalysisPivotPanel extends WizardInnerPanel {

    private static final String SUBJECT_ANALYSIS_PANEL_LAYOUT_XML_FILE = "OVERALL_SUBJECT_ANALYSIS_PANEL_LAYOUT.XML";
    private static final String ITEMS_ANALYSIS_PANEL_LAYOUT_XML_FILE = "OVERALL_ITEMS_ANALYSIS_PANEL_LAYOUT.XML";
    private static final String PROBES_ANALYSIS_PANEL_LAYOUT_XML_FILE = "OVERALL_PROBES_ANALYSIS_PANEL_LAYOUT.XML";

    public static final String SCREEN_ID = "FIFTH";
    private static final String BLANK = "";

    private PivotTable subjectAnalysisPivotTable;
    private PivotTable itemsAnalysisPivotTable;
    private PivotTable probesAnalysisPivotTable;

    private Component itemsAnalysisSeperator;
    private Component probesAnalysisSeperator;
    private Component subjectAnalysisSeperator;

    private Component probesAnalysisButtonBar;
    private Component itemsAnalysisButtonBar;
    private Component subjectAnalysisButtonBar;

    private JScrollPane subjectAnalysisPivotTableScrollPane;
    private JScrollPane itemsAnalysisPivotTableScrollPane;
    private JScrollPane probesAnalysisPivotTableScrollPane;

    private JTabbedPane tableTabs;

    private JPanel subjectAnalysisPanel;
    private JPanel itemsAnalysisPanel;
    private JPanel probesAnalysisPanel;

    private ReloadButton subjectAnalysisButtonBarReloadButton;
    private ReloadButton probesAnalysisButtonBarReloadButton;
    private ReloadButton itemsAnalysisButtonBarReloadButton;

    private ExportButton subjectAnalysisButtonBarExportButton;
    private ExportButton probesAnalysisButtonBarExportbutton;
    private ExportButton itemsAnalysisButtonBarExportButton;

    private FilterButton subjectAnalysisButtonBarFilterButton;
    private FilterButton itemsAnalysisButtonBarFilterButton;
    private FilterButton probesAnalysisButtonBarFilterButton;
    private Integer ignoreAboveMeanTimeValue;
    private Integer ignoreBelowMeanTimeValue;
    private Integer ignoreNTimesAboveStandardDeviationValue;
    private Integer ignoreNTimesBelowStandardDeviationValue;
    private Integer ignoreNTimesAboveZScoreValue;
    private Integer ignoreNTimesBelowZScoreValue;

    /**
     * Helper method to initialize all components on the panel
     */
    private void initComponents() {
	setFilterValues();
	String[] tabsList = TablePanelHandler.getTabsList();
	JTable visibleTable = TablePanelHandler.getTableOnTab(tabsList[tabsList.length - 1]);
	TableModel visibleTableModel = visibleTable.getModel();

	// apply filters here

	TableModelDataConverter dataConverter = new TableModelDataConverter(visibleTableModel);
	dataConverter.startConversion();
	TableModel formattedTableModel = dataConverter.getDataConvertedModel();

	// Seperators
	itemsAnalysisSeperator = DefaultComponentFactory.getInstance().createSeparator("Overall Items Analysis",
		SwingConstants.LEFT);
	probesAnalysisSeperator = DefaultComponentFactory.getInstance().createSeparator("Overall Probes Analysis",
		SwingConstants.LEFT);
	subjectAnalysisSeperator = DefaultComponentFactory.getInstance().createSeparator("Overall Subjet Analysis",
		SwingConstants.LEFT);

	// Buttons above overall subject analysis pivot table
	this.subjectAnalysisButtonBarReloadButton = new ReloadButton();
	this.subjectAnalysisButtonBarExportButton = new ExportButton();
	this.subjectAnalysisButtonBarFilterButton = new FilterButton();

	JButton[] subjectAnalysisButtonBarButtons = { subjectAnalysisButtonBarReloadButton,
		subjectAnalysisButtonBarExportButton, subjectAnalysisButtonBarFilterButton };
	subjectAnalysisButtonBar = ButtonBarFactory.buildLeftAlignedBar(subjectAnalysisButtonBarButtons);

	// Buttons above items analysis pivot table
	this.itemsAnalysisButtonBarReloadButton = new ReloadButton();
	this.itemsAnalysisButtonBarExportButton = new ExportButton();
	this.itemsAnalysisButtonBarFilterButton = new FilterButton();
	JButton[] itemsAnalysisButtonBarButtons = { itemsAnalysisButtonBarReloadButton,
		itemsAnalysisButtonBarExportButton, itemsAnalysisButtonBarFilterButton };
	itemsAnalysisButtonBar = ButtonBarFactory.buildLeftAlignedBar(itemsAnalysisButtonBarButtons);

	// Buttons above probes analysis pivot table
	this.probesAnalysisButtonBarReloadButton = new ReloadButton();
	this.probesAnalysisButtonBarExportbutton = new ExportButton();
	this.probesAnalysisButtonBarFilterButton = new FilterButton();
	JButton[] probesAnalysisButtonBarButtons = { probesAnalysisButtonBarReloadButton,
		probesAnalysisButtonBarExportbutton, probesAnalysisButtonBarFilterButton };
	probesAnalysisButtonBar = ButtonBarFactory.buildLeftAlignedBar(probesAnalysisButtonBarButtons);

	// pivot tables
	subjectAnalysisPivotTable = overallSubjectAnalysis(formattedTableModel);
	itemsAnalysisPivotTable = overallItemsAnalysis(formattedTableModel);
	probesAnalysisPivotTable = overallProbesAnalysis(formattedTableModel);

	subjectAnalysisPivotTableScrollPane = new JScrollPane(subjectAnalysisPivotTable);
	itemsAnalysisPivotTableScrollPane = new JScrollPane(itemsAnalysisPivotTable);
	probesAnalysisPivotTableScrollPane = new JScrollPane(probesAnalysisPivotTable);

	// Base components
	this.tableTabs = new JTabbedPane();
	this.subjectAnalysisPanel = new JPanel();
	this.itemsAnalysisPanel = new JPanel();
	this.probesAnalysisPanel = new JPanel();

	// configure components
	Font font = new Font("sansserif", Font.BOLD, 32);
	itemsAnalysisSeperator.setFont(font);
	probesAnalysisSeperator.setFont(font);
	subjectAnalysisSeperator.setFont(font);

    }

    /**
     * 
     */
    private void setFilterValues() {
	// Read all the user options here:
	PivotWizardPostAnalysisFilterPanelHandler pivotWizardPostAnalysisFilterPanelHandler = PivotWizardPostAnalysisFilterPanelHandler
		.getInstance();
	if (!pivotWizardPostAnalysisFilterPanelHandler.getIgnoreNAboveMeanTime().equals(BLANK)) {
	    ignoreAboveMeanTimeValue = new Integer(pivotWizardPostAnalysisFilterPanelHandler.getIgnoreNAboveMeanTime());
	}
	if (!pivotWizardPostAnalysisFilterPanelHandler.getIgnoreNBelowMeanTime().equals(BLANK)) {
	    ignoreBelowMeanTimeValue = new Integer(pivotWizardPostAnalysisFilterPanelHandler.getIgnoreNBelowMeanTime());
	}
	if (!pivotWizardPostAnalysisFilterPanelHandler.getIgnoreNTimesAboveStandardDeviation().equals(BLANK)) {
	    ignoreNTimesAboveStandardDeviationValue = new Integer(
		    pivotWizardPostAnalysisFilterPanelHandler.getIgnoreNTimesAboveStandardDeviation());
	}
	if (!pivotWizardPostAnalysisFilterPanelHandler.getIgnoreNTimesBelowStandardDeviation().equals(BLANK)) {
	    ignoreNTimesBelowStandardDeviationValue = new Integer(
		    pivotWizardPostAnalysisFilterPanelHandler.getIgnoreNTimesBelowStandardDeviation());
	}
	if (!pivotWizardPostAnalysisFilterPanelHandler.getIgnoreNTimesAboveZScore().equals(BLANK)) {
	    ignoreNTimesAboveZScoreValue = new Integer(
		    pivotWizardPostAnalysisFilterPanelHandler.getIgnoreNTimesAboveZScore());
	}
	if (!pivotWizardPostAnalysisFilterPanelHandler.getIgnoreNTimesBelowZScore().equals(BLANK)) {
	    ignoreNTimesBelowZScoreValue = new Integer(
		    pivotWizardPostAnalysisFilterPanelHandler.getIgnoreNTimesBelowZScore());
	}

    }

    /**
     * @param formattedTableModel
     * @return
     * 
     */
    private PivotTable overallSubjectAnalysis(TableModel formattedTableModel) {

	subjectAnalysisPivotTable = new SubjectAnalysisPivotTable(formattedTableModel,
		UtilityConstants.IS_OVERALL_ANALYSIS).get();
	subjectAnalysisPivotTable.setExportButton(subjectAnalysisButtonBarExportButton);
	subjectAnalysisPivotTable.setReloadButton(subjectAnalysisButtonBarReloadButton);
	subjectAnalysisPivotTable.setFilterButton(subjectAnalysisButtonBarFilterButton);
	return subjectAnalysisPivotTable;

    }

    /**
     * @param formattedTableModel
     * @return
     */
    private PivotTable overallProbesAnalysis(TableModel formattedTableModel) {

	probesAnalysisPivotTable = new ProbesAnalysisPivotTable(formattedTableModel,
		UtilityConstants.IS_OVERALL_ANALYSIS).get();
	probesAnalysisPivotTable.setExportButton(probesAnalysisButtonBarExportbutton);
	probesAnalysisPivotTable.setReloadButton(probesAnalysisButtonBarReloadButton);
	probesAnalysisPivotTable.setFilterButton(probesAnalysisButtonBarFilterButton);
	return probesAnalysisPivotTable;
    }

    /**
     * @param formattedTableModel
     * @return
     */
    private PivotTable overallItemsAnalysis(TableModel formattedTableModel) {

	itemsAnalysisPivotTable = new ItemsAnalysisPivotTable(formattedTableModel, UtilityConstants.IS_OVERALL_ANALYSIS)
		.get();
	itemsAnalysisPivotTable.setExportButton(itemsAnalysisButtonBarExportButton);
	itemsAnalysisPivotTable.setReloadButton(itemsAnalysisButtonBarReloadButton);
	itemsAnalysisPivotTable.setFilterButton(itemsAnalysisButtonBarFilterButton);
	return itemsAnalysisPivotTable;
    }

    /**
     * Helper method to add all initialized components on the panel
     */
    private void addComponents() {

	addSubjectAnalysisPanelComponents();
	addItemsAnalysisPanelComponents();
	addProbesAnalysisPanelComponents();

	// here we add the controls to the container.
	tableTabs.addTab("Subject Analysis", this.subjectAnalysisPanel);
	tableTabs.addTab("Items Analysis", this.itemsAnalysisPanel);
	tableTabs.addTab("Probes Analysis", this.probesAnalysisPanel);

	this.add(tableTabs);

    }

    /**
     * 
     */
    private void addProbesAnalysisPanelComponents() {
	org.mlc.swing.layout.LayoutConstraintsManager layoutConstraintsManager = org.mlc.swing.layout.LayoutConstraintsManager
		.getLayoutConstraintsManager(this.getClass().getResourceAsStream(PROBES_ANALYSIS_PANEL_LAYOUT_XML_FILE));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	probesAnalysisPanel.setLayout(panelLayout);
	probesAnalysisPanel.add(probesAnalysisSeperator, "overallProbesAnalysisSeperator");
	probesAnalysisPanel.add(probesAnalysisButtonBar, "overallProbesAnalysisButtonBar");
	probesAnalysisPanel.add(probesAnalysisPivotTableScrollPane, "overallProbesAnalysisPivotTable");
    }

    /**
     * Helper method to add components on items analysis tab
     */
    private void addItemsAnalysisPanelComponents() {
	org.mlc.swing.layout.LayoutConstraintsManager layoutConstraintsManager = org.mlc.swing.layout.LayoutConstraintsManager
		.getLayoutConstraintsManager(this.getClass().getResourceAsStream(ITEMS_ANALYSIS_PANEL_LAYOUT_XML_FILE));

	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	itemsAnalysisPanel.setLayout(panelLayout);
	itemsAnalysisPanel.add(itemsAnalysisButtonBar, "overallItemsAnalysisButtonBar");
	itemsAnalysisPanel.add(itemsAnalysisSeperator, "overallItemsAnalysisSeperator");
	itemsAnalysisPanel.add(itemsAnalysisPivotTableScrollPane, "overallItemsAnalysisPivotTable");

    }

    /**
     * Helper method to add components on overall subject analysis tab panel
     */
    private void addSubjectAnalysisPanelComponents() {
	org.mlc.swing.layout.LayoutConstraintsManager layoutConstraintsManager = org.mlc.swing.layout.LayoutConstraintsManager
		.getLayoutConstraintsManager(this.getClass()
			.getResourceAsStream(SUBJECT_ANALYSIS_PANEL_LAYOUT_XML_FILE));

	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	subjectAnalysisPanel.setLayout(panelLayout);
	subjectAnalysisPanel.add(subjectAnalysisButtonBar, "overallSubjectAnalysisButtonBar");
	subjectAnalysisPanel.add(subjectAnalysisSeperator, "overallSubjectAnalysisSeperator");
	subjectAnalysisPanel.add(subjectAnalysisPivotTableScrollPane, "overallSubjectAnalysisPivotTable");

    }

    @Override
    public String getPanelId() {
	return SCREEN_ID;
    }

    /**
     * This method is automatically called by WizardPanel when the panel is showed: it can be overridden to add custom
     * logic that must be executed when the panel is showed.
     */
    @Override
    public void init() {
	initComponents();
	addComponents();
    }

    /**
     * This method could be overridden.
     * 
     * @return image name; null if no image is required
     */
    @Override
    public String getImageName() {
	return null;
    }

}
